package com.hdd.hll.quartz.service;

import com.hdd.hll.modules.card.bean.Card;
import com.hdd.hll.quartz.entity.JobInfo;
import com.hdd.hll.quartz.entity.TriggerInfo;
import com.hdd.hll.quartz.job.TestJob;
import com.hdd.hll.quartz.jobbuilder.CustomJobBuilder;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

@Slf4j
@Service
public class TestJobService {
    private static final String JD_TRIGGER_GROUP_NAME = "JD_SCHEDULE_GROUP";
    private static final String JD_JOB_GROUP_NAME = "JD_SCHEDULE_GROUP";
    private static final String JD_JOB_PREFIX = "JD_JOB_";
    private static final String PUSH_WEIGHT_BILL = "PUSH_WEIGHT_BILL";


    @Autowired
    private CustomJobBuilder<Card> customJobBuilder;

    /**
     * 说明：以下为模拟流程，实际过程中只要一个就可以了
     * 每一组job的group一样，但是job和trigger的name不能和上一次一样，必须唯一
     */
    public void registerJob() {
        log.info("注册了一个job, {}", LocalDateTime.now());
        // 模拟添加第二个job
        String suffix = UUID.randomUUID().toString().replace("-","");

        JobInfo jobInfo = new JobInfo();
        jobInfo.setName(JD_JOB_PREFIX + suffix);
        jobInfo.setGroup(JD_JOB_GROUP_NAME);


        TriggerInfo triggerInfo = new TriggerInfo();
        triggerInfo.setName(PUSH_WEIGHT_BILL + suffix);
        triggerInfo.setGroup(JD_TRIGGER_GROUP_NAME);

        Card card = new Card();
        card.setCardNo("20001111111");

//        try {
//            Thread.sleep(1000 * 20);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        // 模拟添加第二个job
//        String suffix1 = UUID.randomUUID().toString().replace("-","");
//
//        JobInfo jobInfo1 = new JobInfo();
//        jobInfo1.setName(JD_JOB_PREFIX + suffix1);
//        jobInfo1.setGroup(JD_JOB_GROUP_NAME);
//
//        TriggerInfo triggerInfo1 = new TriggerInfo();
//        triggerInfo1.setName(PUSH_WEIGHT_BILL + suffix1);
//        triggerInfo1.setGroup(JD_TRIGGER_GROUP_NAME);
//
//        Card card1 = new Card();
//        card1.setCardNo("20002222222");
        try {
            customJobBuilder.register(jobInfo, triggerInfo, card, TestJob.class);
//            customJobBuilder.register(jobInfo1, triggerInfo1, card1, TestJob.class);
        } catch (SchedulerException e) {
            log.error("注册Job失败:{}", e.getMessage());
        }
    }
}

